#include <xp/msw/scm.h>
#include <xp/bas/stdio.h>
#include <xp/bas/time.h>

int xp_main ()
{
	xp_scm_t scm;
	xp_svcstatus_t status;

	
	if (xp_scm_open(&scm, XP_NULL) == XP_NULL) {
		xp_fprintf (xp_stderr, XP_TEXT("cannot open scm\n"));
		return -1;
	}


	if (xp_scm_stopsvc (&scm, XP_TEXT("wuauserv")) == -1) {
		if (xp_scm_geterrnum(&scm) != XP_SCM_ENOACT) {
			xp_fprintf (xp_stderr, 
				XP_TEXT("cannot stop service - %s\n"), 
				xp_scm_geterrstr(&scm));
			return -1;
		}
	}

	if (xp_scm_getsvcstatus (&scm, XP_TEXT("wuauserv"), &status)  == -1) {
		xp_fprintf (xp_stderr, 
			XP_TEXT("cannot get service status - %s\n"), 
			xp_scm_geterrstr(&scm));
	}

	xp_sleep (1000);

	if (xp_scm_getsvcstatus (&scm, XP_TEXT("wuauserv"), &status)  == -1) {
		xp_fprintf (xp_stderr, 
			XP_TEXT("cannot get service status - %s\n"), 
			xp_scm_geterrstr(&scm));
	}

	if (xp_scm_startsvc (&scm, XP_TEXT("wuauserv")) == -1) {
		xp_fprintf (xp_stderr, 
			XP_TEXT("cannot start service - %s\n"),
			xp_scm_geterrstr(&scm));
		return -1;
	}

	xp_sleep (1000);
	if (xp_scm_getsvcstatus (&scm, XP_TEXT("wuauserv"), &status)  == -1) {
		xp_fprintf (xp_stderr, 
			XP_TEXT("cannot get service status - %s\n"), 
			xp_scm_geterrstr(&scm));
	}
	else {
		xp_printf (XP_TEXT("type = 0x%x, state = 0x%x\n"), status.type, status.state);
	}

	xp_printf (XP_TEXT("service restarted\n"));

	xp_scm_close (&scm);
	return 0;
}
